/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_footprint.service;


import com.yuecai.modules.yc_footprint.domain.YcFootprint;
import com.yuecai.modules.yc_footprint.repository.YcFootprintRepository;
import com.yuecai.modules.yc_footprint.service.dto.YcFootprintDto;
import com.yuecai.modules.yc_footprint.service.dto.YcFootprintQueryCriteria;
import com.yuecai.modules.yc_footprint.service.mapstruct.YcFootprintMapper;
import com.yuecai.modules.yc_footprint.vo.YcFootprintVo;
import com.yuecai.modules.yc_goods.domain.YcGoods;
import com.yuecai.modules.yc_goods.repository.YcGoodsRepository;
import com.yuecai.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-07-02
**/
@Service
@RequiredArgsConstructor
public class YcFootprintService {

    private final YcFootprintRepository ycFootprintRepository;
    private final YcFootprintMapper ycFootprintMapper;
    private final YcGoodsRepository ycGoodsRepository;

    
    public long finCount(YcFootprintQueryCriteria criteria, Pageable pageable){
        Page<YcFootprint> page = ycFootprintRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return page.getTotalElements();
    }


    public Map<String,Object> queryAll(YcFootprintQueryCriteria criteria, Pageable pageable){
        Page<YcFootprint> page = ycFootprintRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        List<YcFootprint> content = page.getContent();
        List<YcFootprintVo> list = new ArrayList<>();
        for (YcFootprint ycFootprint : content) {
            Long goodId = ycFootprint.getGoodId();
            YcGoods goods = ycGoodsRepository.findByIdAndStatusTrue(goodId);
            if (goods!=null){
                YcFootprintVo ycFootprintVo = new YcFootprintVo();
                BeanUtils.copyProperties(goods,ycFootprintVo);
                list.add(ycFootprintVo);
            }else {
                ycFootprintRepository.deleteById(ycFootprint.getId());
            }
        }

        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",list);
        map.put("totalElements",page.getTotalElements());
        return map;
    }





    @Transactional(rollbackFor = Exception.class)
    public YcFootprintDto create(YcFootprint resources) {
        List<YcFootprint> list = ycFootprintRepository.findByUserIdOrderByCreateTime(resources.getUserId());
        int size = list.size();
        if (size>100){
            ycFootprintRepository.delete(list.get(0));
        }
        YcFootprint save = ycFootprintRepository.save(resources);

        return ycFootprintMapper.toDto(save);
    }

}